package jp.bsws.action.syukei;

/*
 * ログインサンプル
 */
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.KeyValueBean;
import jp.bsws.bean.MasterBean;
import jp.bsws.bean.NikkeiJohoBean;
import jp.bsws.bl.BSWSBl;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.GenkinShutsunoDao;
import jp.bsws.dao.ShiharaiHohoDao;
import jp.bsws.dao.UriageDao;
import jp.bsws.dao.UriageShubetsuDao;
import jp.bsws.model.GenkinShutsunoModel;
import jp.bsws.model.ShiharaiHohoModel;
import jp.bsws.model.UriageNikkeiModel;
import jp.bsws.model.UriageShubetsuModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
/*
 * メインメニューアクションクラス
 */
public class  NikkeiHyoAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;
	private String strSansyoYmd;
	private String strSansyoTenpoCod;
	private String strSansyoJugyoinKnj;
	private String strSansyoShimeFlg;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	// ログインユーザ
	private List<KeyValueBean> kvTenpoList;
	private NikkeiJohoBean nikkeiHyoJoho;

	private List<MasterBean> uriageShubetsuHList;
	private List<MasterBean> uriageShiharaiHohoHList;
	private List<MasterBean> shiharaiHohoHList;
	private List<MasterBean> genkinShutsunoHList;

	public String execute() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();
		try{

			//画面表示内容の設定
			hyojiJunbi(con);

		}catch(SQLException e){
			con.rollback();
			throw e;
		}finally{
			con.close();
		}
		return SUCCESS;
	}

	public String preDD() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();
		try{
			//日付を1日前に
			int year =  Integer.parseInt(strSansyoYmd.substring(0,4));
			int month =  Integer.parseInt(strSansyoYmd.substring(4,6));
			int date =  Integer.parseInt(strSansyoYmd.substring(6,8));
			Calendar cal = Calendar.getInstance();
			cal.set(year, month - 1, date);
			cal.add(Calendar.DATE,-1);
			Date currentTime = cal.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd");
			strSansyoYmd = sdf.format(currentTime);

			//画面表示内容の設定
			hyojiJunbi(con);

		}finally{
			con.close();
		}
		return SUCCESS;
	}

	public String nextDD() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();
		try{
			//日付を1日後に
			int year =  Integer.parseInt(strSansyoYmd.substring(0,4));
			int month =  Integer.parseInt(strSansyoYmd.substring(4,6));
			int date =  Integer.parseInt(strSansyoYmd.substring(6,8));
			Calendar cal = Calendar.getInstance();
			cal.set(year, month - 1, date);
			cal.add(Calendar.DATE,1);
			Date currentTime = cal.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd");
			strSansyoYmd = sdf.format(currentTime);

			//画面表示内容の設定
			hyojiJunbi(con);

		}finally{
			con.close();
		}
		return SUCCESS;
	}

	public String shimeNikkei() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();

		try{

			BSWSBl bSWSBl = new BSWSBl();
			bSWSBl.updateNikkeiJoho(con, bSWSSession, strSansyoTenpoCod, strSansyoYmd, strSansyoShimeFlg);

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(Exception se){
			con.rollback();
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws SQLException,Exception{

		//参照可能店舗リスト
		this.kvTenpoList = bSWSSession.getSansyoTenpoList();

		//参照店舗がnullの場合、セッションから店舗を取得
		if(BSWSUtils.isBlankNull(strSansyoTenpoCod)){
			strSansyoTenpoCod = bSWSSession.getStrTenpoCod();
		}else{
			bSWSSession.setStrTenpoCod(strSansyoTenpoCod) ;
		}

		//日付を取得
		if(BSWSUtils.isBlankNull(strSansyoYmd)){
			strSansyoYmd = BSWSUtils.getSysDateYmd();
		}

		//締め情報
//		JugyoinDao jugyoinDao = JugyoinDao.getInstance();
//		this.kvJugyoinList =  jugyoinDao.getJugyoinList(con, bSWSSession.getStrGroupCod(), strSansyoTenpoCod );
		//締め担当者と締め状態か確認
		UriageDao uriageDao = UriageDao.getInstance();
		UriageNikkeiModel uriageNikkeiModel = uriageDao.getUriageNikkeiRow(con, bSWSSession.getStrGroupCod(), strSansyoTenpoCod, Integer.parseInt(strSansyoYmd));

		if(uriageNikkeiModel != null){
			strSansyoJugyoinKnj = uriageNikkeiModel.getStrJugyoinKnj();
			strSansyoShimeFlg = uriageNikkeiModel.getStrShimeFlg();
		}else{
			strSansyoShimeFlg = "0";
		}

		//マスタ情報取得
		UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();
		ShiharaiHohoDao shiharaiHohoDao = ShiharaiHohoDao.getInstance();
		GenkinShutsunoDao genkinShutsunoDao = GenkinShutsunoDao.getInstance();

		//売上種別情報取得
		List<UriageShubetsuModel> uriageShubetsuList= uriageShubetsuDao.getUriageShubetsuJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
		//支払方法情報取得
		List<ShiharaiHohoModel> shiharaiHohoList= shiharaiHohoDao.getShiharaiHohoJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
		//現金出納情報取得
		List<GenkinShutsunoModel> genkinShutsunoList = genkinShutsunoDao.getGenkinShutsunoJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);

		uriageShubetsuHList = new ArrayList<MasterBean>();
		for(UriageShubetsuModel uriageShubetsuModel : uriageShubetsuList) {
			if(BSWSConstants.C_HYOJI.equals(uriageShubetsuModel.getStrUriageShubetsuHyojiFlg())){
				MasterBean masterBean = new MasterBean();
				masterBean.setIntSeq(uriageShubetsuModel.getSeq());
				masterBean.setStrValue(uriageShubetsuModel.getStrUriageShubetsuKnj());
				uriageShubetsuHList.add(masterBean);
			}
		}
		uriageShiharaiHohoHList = new ArrayList<MasterBean>();
		shiharaiHohoHList = new ArrayList<MasterBean>();
		for(ShiharaiHohoModel shiharaiHohoModel : shiharaiHohoList) {
			if(BSWSConstants.C_HYOJI.equals(shiharaiHohoModel.getStrShiharaiHohoHyojiFlg())){
				MasterBean masterBean = new MasterBean();
				masterBean.setIntSeq(shiharaiHohoModel.getSeq());
				masterBean.setStrValue(shiharaiHohoModel.getStrShiharaiHohoRyakuKnj());
				masterBean.setHontaiKeijoFlg(shiharaiHohoModel.getStrShiharaiHohoHontaiKeijoFlg());
				shiharaiHohoHList.add(masterBean);
			}
		}
		genkinShutsunoHList = new ArrayList<MasterBean>();
		for(GenkinShutsunoModel genkinShutsunoModel : genkinShutsunoList) {
			if(BSWSConstants.C_HYOJI.equals(genkinShutsunoModel.getStrGenkinShutsunoHyojiFlg())){
				MasterBean masterBean = new MasterBean();
				masterBean.setIntSeq(genkinShutsunoModel.getSeq());
				masterBean.setStrValue(genkinShutsunoModel.getStrGenkinShutsunoKnj());
				genkinShutsunoHList.add(masterBean);
			}
		}

		BSWSBl bSWSBl = new BSWSBl();
		//日計表情報取得
		nikkeiHyoJoho = bSWSBl.getNikkeiJoho(con, bSWSSession,
				uriageShubetsuHList,
				shiharaiHohoHList,
				genkinShutsunoHList,
				strSansyoTenpoCod, strSansyoYmd);
	}

	public String getStrSansyoYmd() {
		return strSansyoYmd;
	}

	public void setStrSansyoYmd(String strSansyoYmd) {
		this.strSansyoYmd = strSansyoYmd;
	}

	public List<KeyValueBean> getKvTenpoList() {
		return kvTenpoList;
	}

	public void setKvTenpoList(List<KeyValueBean> kvTenpoList) {
		this.kvTenpoList = kvTenpoList;
	}

	public BSWSSession getBSWSSession() {
		return bSWSSession;
	}

	public void setBSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public String getStrSansyoTenpoCod() {
		return strSansyoTenpoCod;
	}

	public void setStrSansyoTenpoCod(String strSansyoTenpoCod) {
		this.strSansyoTenpoCod = strSansyoTenpoCod;
	}

	public List<MasterBean> getUriageShubetsuHList() {
		return uriageShubetsuHList;
	}

	public void setUriageShubetsuHList(List<MasterBean> uriageShubetsuHList) {
		this.uriageShubetsuHList = uriageShubetsuHList;
	}

	public List<MasterBean> getShiharaiHohoHList() {
		return shiharaiHohoHList;
	}

	public void setShiharaiHohoHList(List<MasterBean> shiharaiHohoHList) {
		this.shiharaiHohoHList = shiharaiHohoHList;
	}

	public NikkeiJohoBean getNikkeiHyoJoho() {
		return nikkeiHyoJoho;
	}

	public void setNikkeiHyoJoho(NikkeiJohoBean nikkeiHyoJoho) {
		this.nikkeiHyoJoho = nikkeiHyoJoho;
	}

	public List<MasterBean> getGenkinShutsunoHList() {
		return genkinShutsunoHList;
	}

	public void setGenkinShutsunoHList(List<MasterBean> genkinShutsunoHList) {
		this.genkinShutsunoHList = genkinShutsunoHList;
	}

	public List<MasterBean> getUriageShiharaiHohoHList() {
		return uriageShiharaiHohoHList;
	}

	public void setUriageShiharaiHohoHList(List<MasterBean> uriageShiharaiHohoHList) {
		this.uriageShiharaiHohoHList = uriageShiharaiHohoHList;
	}

	public String getStrSansyoJugyoinKnj() {
		return strSansyoJugyoinKnj;
	}

	public void setStrSansyoJugyoinKnj(String strSansyoJugyoinKnj) {
		this.strSansyoJugyoinKnj = strSansyoJugyoinKnj;
	}

	public String getStrSansyoShimeFlg() {
		return strSansyoShimeFlg;
	}

	public void setStrSansyoShimeFlg(String strSansyoShimeFlg) {
		this.strSansyoShimeFlg = strSansyoShimeFlg;
	}


}